package bookManager;

import com.sun.org.apache.regexp.internal.RE;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookDao {
    public void add(Book book){
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            connection=DBUtil.getConnection();
            String sql="insert into book values(null,?,?,?,?,0)";
            statement=connection.prepareStatement(sql);
            statement.setString(1,book.getName());
            statement.setString(2,book.getAuthor());
            statement.setInt(3,book.getPrice());
            statement.setString(4,book.getType());
            int ret=statement.executeUpdate();
            if(ret!=1){
                System.out.println("新增书籍失败！");
            }else {
                System.out.println("新增书籍成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }

    public List<Book> findBooks(){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        List<Book> books=new ArrayList<>();
        try {
            connection=DBUtil.getConnection();
            String sql="select * from book";
            statement=connection.prepareStatement(sql);
            resultSet=statement.executeQuery();
            while (resultSet.next()){
                Book book=new Book();
                book.setBookId(resultSet.getInt("bookId"));
                book.setName(resultSet.getString("name"));
                book.setAuthor(resultSet.getString("author"));
                book.setPrice(resultSet.getInt("price"));
                book.setType(resultSet.getString("type"));
                book.setBorrowed(resultSet.getInt("isBorrowed")==1);
                books.add(book);
            }
            return books;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return books;
    }

    public List<Book> findBooksByName(String name){

        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        List<Book> books=new ArrayList<>();
        try {
            connection=DBUtil.getConnection();
            String sql="select * from book where name=?";
            statement=connection.prepareStatement(sql);
            statement.setString(1,name);
            resultSet=statement.executeQuery();
            while (resultSet.next()) {
                Book book = new Book();
                book.setBookId(resultSet.getInt("bookId"));
                book.setName(resultSet.getString("name"));
                book.setAuthor(resultSet.getString("author"));
                book.setPrice(resultSet.getInt("price"));
                book.setType(resultSet.getString("type"));
                book.setBorrowed(resultSet.getInt("isBorrowed") == 1);
                books.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return books;
    }

    public boolean delete(int bookId){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            connection=DBUtil.getConnection();
            String sql="delete from book where bookId=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,bookId);
            int ret=statement.executeUpdate();
            if(ret!=1){
                return false;
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return false;
    }

    public boolean borrowBook(int bookId){
        Connection connection=null;
        PreparedStatement statement=null;
        PreparedStatement statement2=null;
        ResultSet resultSet=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from book where bookId=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,bookId);
            resultSet=statement.executeQuery();
            if(resultSet.next()){
                boolean isBorrowed=(resultSet.getInt("isBorrowed")==1);
                if(isBorrowed){
                    System.out.println("书已经被借出");
                    return false;
                }
            }else{
                System.out.println("书不存在");
                return false;
            }
            sql="update book set isBorrowed=1 where bookId=?";
            statement2=connection.prepareStatement(sql);
            statement2.setInt(1,bookId);
            int ret=statement2.executeUpdate();
            if(ret!=1){
                System.out.println("修改借出状态失败");
                return false;
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                statement2.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public boolean returnBook(int bookId){
        Connection connection=null;
        PreparedStatement statement=null;
        PreparedStatement statement2=null;
        ResultSet resultSet=null;
        try {
            connection=DBUtil.getConnection();
            String sql="select * from book where bookId=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,bookId);
            resultSet=statement.executeQuery();
            if(resultSet.next()){
                boolean isBorrowed=(resultSet.getInt("isBorrowed")==1);
                if(!isBorrowed){
                    System.out.println("书已经还回来");
                    return false;
                }
            }else{
                System.out.println("书不存在");
                return false;
            }
            sql="update book set isBorrowed=1 where bookId=?";
            statement2=connection.prepareStatement(sql);
            statement2.setInt(1,bookId);
            int ret=statement2.executeUpdate();
            if(ret!=1){
                System.out.println("修改还回状态失败");
                return false;
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                statement2.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
